package ch2linkedlist;

import static ch2linkedlist.Chapter.*;

public class Ex5 {

	static ListNode findStart(ListNode head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		
		ListNode r1 = head.next;
		ListNode r2 = head.next.next;
		while (r2.next != null && r1 != r2) {
			r1 = r1.next;
			if (r2.next.next == null) {
				return null;
			}
			r2 = r2.next.next;
		}

		if (r2.next == null) {
			return null;
		}
		
		r1 = head;
		while (r2 != null && r1 != r2) {
			r1 = r1.next;
			r2 = r2.next;
		}
		
		return r1;
	}
	
	public static void main(String ... argv) {

		ListNode head = createSampleList(new int [] {1, 2, 3, 4, 5});
		
//		head.next.next.next.next.next = head.next.next;
		
//		print(head);
		
		ListNode start = findStart(head);
		
		System.out.println(start);
		
	}
	
}
